package com.xuegao.xuegaololskills.swing;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class MyPanel extends JScrollPane {
    JTextArea text = new JTextArea();  //创建文本区域

    public MyPanel() {
        this.setFocusable(true);

        text.setBounds(5, 5, 300, 200); //设置文本区域的大小

        text.addKeyListener(new key_hander());

        this.add(text);

    }

    //创建响应键盘事件监听器类

    class key_hander implements KeyListener {
        @Override

        public void keyPressed(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                int code = e.getKeyCode();

                if (code == KeyEvent.VK_F1) {
                    text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键按下\n");

                }

            }

        }

        @Override

        public void keyReleased(KeyEvent e) {
            //text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键松开\n");

        }

        // f卡头的按键不会走下面
        @Override
        public void keyTyped(KeyEvent e) {
            //text.append("输入内容:" + e.getKeyChar() + "\n");

            if (e.getID() == KeyEvent.KEY_TYPED) {
                char ch = e.getKeyChar();

                text.append("输入内容:" + e.getKeyChar() + "\n");

            }

        }

    }

}